package com.wu;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class RestFulController {

     //映射访问路径 http://localhost:8080/mvc/commit/1/2
             @RequestMapping("/commit/{p1}/{p2}")
    //访问路径 http://localhost:8080/mvc/commit/1/edit/3
//             @RequestMapping("/commit/{p1}/edit/{p2}")
             public String index(@PathVariable int p1, @PathVariable int p2, Model model){
             int result = p1+p2;
             //Spring MVC会自动实例化一个Model对象用于向视图中传值
             model.addAttribute("msg", "结果："+result);
             //返回视图位置
             return "test";
   }

    /**
     * 访问路径 http://localhost:8080/mvc/commit/?p1=1&p2=2
     * 不同点2 ,还有默认值
     * http://localhost:8080/mvc/commit/?p2=3
     * @param p1
     * @param p2
     * @param model
     * @return
     */
    @RequestMapping("/commit")
    public String index2(@RequestParam(value = "p1",required = false,defaultValue = "0") int p1, @RequestParam(value ="p2") int p2, Model model){
        int result = p1+p2;
        //Spring MVC会自动实例化一个Model对象用于向视图中传值
        model.addAttribute("msg", "结果："+result);
        //返回视图位置
        return "test";
    }
}
